package org.steve.betterattack.Listener;

import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.steve.betterattack.BetterAttack;

public class WeaponListener implements Listener {
    final BetterAttack plugin;
    public WeaponListener(BetterAttack plugin){
        this.plugin=plugin;
    }
    @EventHandler(priority = EventPriority.HIGH)
    public void onAttack(EntityDamageByEntityEvent event){
        if(event.getDamager() instanceof Player){
            Player player=(Player) event.getDamager();
            ItemStack item= player.getInventory().getItem(player.getInventory().getHeldItemSlot());
            {
                if (item==null)return;
                ItemMeta im=item.getItemMeta();
                if (im==null)return;
                String id=im.getPersistentDataContainer().get(new NamespacedKey(plugin,"id"), PersistentDataType.STRING);
                if(id!=null&&plugin.RegisteredWeapons.containsKey(id)){
                    double damageTimes=1;
                    double critTimes;
                    int level=im.getPersistentDataContainer().get(new NamespacedKey(plugin,"level"), PersistentDataType.INTEGER);

                    critTimes= plugin.RegisteredWeapons.get(id).getCritTimes(level);
                    damageTimes+=critTimes;
                    if (critTimes!=0){
                        player.sendMessage("暴击！多造成了"+String.format("%.2f",critTimes*event.getDamage())+"点伤害");
                    }
                    event.setDamage(event.getDamage()*damageTimes);
                }
            }
        }
    }
    @EventHandler
    public void onAbilityExecute(PlayerDropItemEvent event){
        if (event.getItemDrop().getItemStack().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(plugin,"type"),PersistentDataType.STRING)=="weapon"){

        }
    }
}
